import com from './addon/com'
import selecthelper from './addon/selecthelper'
import schools from './addon/schools'

var components = com([
	'HeaderLayout',
	'HeaderDock',
	'LayoutMain',
	'LayoutBox',
	'Btn',
	'Icon',
	'FormGroup',
	'FormInput',
	'FormSelect',
	'FormCode',
	'GridRow'
]);


new Vue({
	el: '#app',
	components: components,
	created: function(){
		var _this = this;

		var d = [];
		for(var i=0; i<SCHOOLS.length; i++){
			var item = {
				val: SCHOOLS[i].id,
				text: SCHOOLS[i].name,
				type: SCHOOLS[i].type
			}
			if(SCHOOLS[i].id == USERINFO.school){
				item.selected = 'selected'
			}
			d.push(item);
		}
		_this.schools = d;


		_this.grades = selecthelper.translate(GRADES, {
			val: 'id',
			text: 'name'
		}, USERINFO.grade);
		_this.classes = selecthelper.translate(CLASSES, {
			val: 'id',
			text: 'name'
		}, USERINFO.class);
		//console.log(this.schools)

	},
	data (){
		return {
			 userinfo: USERINFO,
			 schools: [],
			 grades: [],
			 classes: []
		}
	},
	methods: {
		schoolchange: function(obj){
			var _this = this;
			var type = '';
			var sh = this.schools;
			this.grades = [];
			this.classes = [];
			this.schoolid = obj.val;
			this.$refs.grades.setVal('')
			this.$refs.classes.setVal('')
			for(var key=0; key<sh.length; key++){
				if(this.schools[key].val == obj.val){
					type = this.schools[key].type;
					this.schooltype = type;
				}
			}
			schools.getGrades(SITE_URL + '/passport/getgrades', {
				type: type
			}, function(d){
				_this.grades = d;
			});
		},
		gradechange: function(obj){
			var _this = this;
			this.classes = [];
			this.$refs.classes.setVal('')
			schools.getClasses(SITE_URL + '/passport/getclasses', {
				school: _this.$refs.op.val,
				grade: obj.val
			}, function(d){
				_this.classes = d;
			});
		},
		submit: function(){
			this.$broadcast('form-verify', '');
			if(this.$refs.form.pass){
				var data = this.$refs.form.data;


				//console.log('sending data')
				//console.log(this.$refs.form.data);
				$.ajax({
					url: SITE_URL + '/user/update',
					type:'post',
					data: this.$refs.form.data,
					success: function(data){
						if(!data.code){
							alert(data.msg);
						} else {
							alert(data.msg);
						}
					},
					error: function(){
						alert('系统错误')
					}
				});
			} else {
				//this.$refs.myform.setNotice('账号密码不正确')
				console.log('data wrong')
			}
		}
	}
})
